package com.shuorikeji.huohuo.ui.mh;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.MrtjMhAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhInfoBean;
import com.shuorikeji.huohuo.bean.MhInfoMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.ui.sj.MhSdDetailActivity;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhEverydayTjActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;

    private int curMhPageNum;
    private List<MhInfoBean> mhList = new ArrayList<>();
    private MrtjMhAdapter mrtjMhAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_everyday_tj);
    }

    @Override
    protected void initData() {
        curMhPageNum = 1;
        initSmartRefreshLayout();
        initAdapter();
        getAllMh();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(MhEverydayTjActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curMhPageNum = 1;
                getAllMh();
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curMhPageNum = curMhPageNum + 1;
                getAllMh();
            }
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(MhEverydayTjActivity.this, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        mrtjMhAdapter = new MrtjMhAdapter(MhEverydayTjActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhInfoBean data = (MhInfoBean) object;
                        Intent intent = new Intent(MhEverydayTjActivity.this, MhSdDetailActivity.class);
                        intent.putExtra("SD_ID", data.getId());
                        startActivity(intent);
                        break;
                }
            }
        });
        rvData.setAdapter(mrtjMhAdapter);
    }

    /**
     * 获取所有漫画
     */
    private void getAllMh() {
        mPresenter.getAllMh(5, -1, 20, curMhPageNum, "", "", -1, -1, -1, -1,
                -1, -1, -1, -1, 1, -1, true, new ClickCallBack() {
                    @Override
                    public void onClick(int flag, Object object) {
                        switch (flag) {
                            case 1:
                                srlLayout.finishRefresh();
                                srlLayout.finishLoadMore();
                                MhInfoMainBean data = (MhInfoMainBean) object;
                                if (curMhPageNum == 1) {
                                    mhList.clear();
                                }
                                mhList.addAll(data.getData().getData());
                                mrtjMhAdapter.setData(mhList);
                                break;
                            case 2:
                                srlLayout.finishRefresh();
                                srlLayout.finishLoadMore();
                                String msg = (String) object;
                                ToastShowUtil.showToastCenter(MhEverydayTjActivity.this, msg);
                                break;
                        }
                    }
                });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
    }
}